/**
 * Created by Administrator on 2016/11/14 0014.
 */
$(function () {
    $.weChat = function (title, desc, url,imgUrl,link) {
        var appId = "",
            timestamp = "",
            nonceStr = "",
            signature = "",
            imgUrl = imgUrl || "http://sing.p2nr.com/voice/res/web/img/weChat-share.jpg",
            link   = link || "http://sing.p2nr.com/voice/shareIndex",
            signUrl = location.href.split('#')[0];

        $.ajax({
            url: url,
            type: "post",
            data: {"signUrl": encodeURIComponent(signUrl)},
            dataType: "json",
            async: false,
            error: function () {
                alert("网络延迟，请稍后！");
                return false;
            },
            success: function (data) {
                appId = data.result.appId;
                timestamp = data.result.timestamp;
                nonceStr = data.result.nonceStr;
                signature = data.result.signature;
            }
        });
        wx.config({
            debug: false,
            appId: appId,
            timestamp: timestamp,
            nonceStr: nonceStr,
            signature: signature,
            jsApiList: [
                'checkJsApi',
                'onMenuShareTimeline',
                'onMenuShareAppMessage'
            ]
        });

        wx.ready(function () {
            //分享给朋友的
            wx.onMenuShareAppMessage({
                title: title, // 分享标题
                desc: desc, // 分享描述
                link: link, // 分享链接
                imgUrl: imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
            //分享给朋友圈
            wx.onMenuShareTimeline({
                title: title, // 分享标题
                link: link, // 分享链接
                imgUrl: imgUrl, // 分享图标
                success: function () {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function () {
                    // 用户取消分享后执行的回调函数
                }
            });
        });

        wx.error(function (res) {
            //alert(res);
        });
    }

    function onBridgeReady(appId, timeStamp, nonceStr, pack, signType, paySign) {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId": appId,     //公众号名称，由商户传入
                "timeStamp": timeStamp,         //时间戳，自1970年以来的秒数
                "nonceStr": nonceStr, //随机串
                "package": pack,
                "signType": signType,         //微信签名方式：
                "paySign": paySign //微信签名
            },
            function (res) {
                //WeixinJSBridge.log(res.err_msg);
                //alert(res.err_code + res.err_desc + res.err_msg);
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    //支付成功  关注柒牌文化
                    var codeMode = $.modal({
                        extraClass:"nv-modal-service",
                        text:  '<div class="nv-modal-code">'+
                                    '<strong>投票成功</strong>'+
                                    '<p>长按扫描二维码，查看最新赛事信息</p>'+
                                    '<div class="img-wrap">'+
                                        '<img src="res/web/img/code-public.png"/>'+
                                    '</div>'+
                                    '<p>官方公众号</p>'+
                                '</div>'
                    });
                    $(codeMode).on('close',function(){
                        window.location.reload()
                    })
                } else {
                    //用户取消支付
                    //$.closeModal();
                    $.toast("支付失败...");
                }
            }
        );
    }

    $.wePay = function pay(appId, timeStamp, nonceStr, pack, signType, paySign) {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady(appId, timeStamp, nonceStr, pack, signType, paySign), false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady(appId, timeStamp, nonceStr, pack, signType, paySign));
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(appId, timeStamp, nonceStr, pack, signType, paySign));
            }
        } else {
            onBridgeReady(appId, timeStamp, nonceStr, pack, signType, paySign);
        }
    }

});
